package com.atstudy.ticket;

/**
 * @version v1.0
 * @description:
 * @author: admin
 * @date: 2022/12/23 9:48
 * @copyright: 博为峰
 */
public class Ticket02 implements Runnable {

    public int num = 100;
    public boolean flag = true;

    /*
       线程安全问题的原因：
       1.多个线程同时操作共享数据
       2.操作共享数据的代码有多行

       锁
    */

    @Override
    public void run() {
        while (flag) {//还是最后一张票
            if (num > 0) {//t0  t1  t2   t3
                          // 0  -1  -2  -3
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "," + num--);
            } else {
                flag = false;
            }
        }
    }
}
